<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
    array('op'=>'list','title'=>'留言列表'),
    array('op'=>'formlist','title'=>'自定义表单列表'),
    array('op'=>'formlist2','title'=>'显示项目字段表单'),
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type <>'=>1);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_account_message',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y/m/d',$item['createtime']);
			$item['title']=pdo_getcolumn('core_account_article',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['aid']),'title');
			$item['title'] = $item['title'] ? $item['title'] : '非文章留言';
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'username','title'=>'姓名','type'=>'string'),
			array('name'=>'userphone','title'=>'电话','type'=>'string'),
			array('name'=>'content','title'=>'留言内容','type'=>'string'),
			array('name'=>'title','title'=>'所属文章','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search_one'=>array('name'=>'username','help'=>'客户姓名','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_message',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'留言',
		'tabs'=>$tabs,
		'edit'=>array(
			array('type'=>'copy','title'=>'留言姓名','name'=>'username', 'help'=>'' ),
			array('type'=>'copy','title'=>'电话','name'=>'userphone', 'help'=>'' ),
			array('type'=>'copy','title'=>'邮箱','name'=>'useremail', 'help'=>'' ),
			array('type'=>'copy','title'=>'内容','name'=>'content', 'help'=>'' ),
			array('type'=>'copy','title'=>'留言时间','name'=>'createtime', 'help'=>'' ),
			array('type'=>'switch','title'=>'状态','name'=>'status', 'help'=>'' ),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'status'=>trim($postdata['status']),
		'content'=>trim($postdata['content'])
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('core_account_message',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('留言修改成功！');
	}else{
		exi('没有找到相关留言');
	}
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($_RQ['id']);
	pdo_update('core_account_message',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_account_message',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_account_message',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='formlist'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['type']=1;
    if($_RQ['mid']){
        $params['uid']=intval($_RQ['mid']);
        $return['params']['mid']=$_RQ['mid'];
    }
	if(!empty($_RQ['plugin_name'])){
		$params['plugin_name']=trim($_RQ['plugin_name']);
		$return['params']['plugin_name']=$_RQ['plugin_name'];
	}
	if(!empty($_RQ['pageaction'])){
		$params['pageaction']=trim($_RQ['pageaction']);
		$return['params']['pageaction']=$_RQ['pageaction'];
	}
    if($_RQ['formid']){
        $params['formid like']='%id'.trim($_RQ['formid']).'i%';
        $return['params']['formid']=$_RQ['formid'];
    }
    //创建时间
    if($_RQ['createtime']){
        $time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
        $time_range=json_decode($time_range,true);
        $params['createtime between']=array($time_range['tstart'],$time_range['tend']);
        $return['params']['createtime']=$time_range;
    }
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_account_message',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
            $item['member']=cfc('core')->member_show($item['uid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
        'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：建议先选择页面，再导出表单，否则表单的自定义项的名称，会错乱！','edit'=>false,'list'=>array(
            array('name'=>'title','title'=>'页面','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'createtime','title'=>'留言时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','op'=>'look','title'=>'留言详情'),
		)),
		'search'=>array(
            array('type'=>'choose','width'=>18,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
            array('type'=>'choose','width'=>18,'action'=>'core/account.message/diy/','name'=>'formid','title'=>'选择页面'),
            array('name'=>'createtime','width'=>15,'title'=>'提交时间','type'=>'time_range'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='formlist2'){
    $params=array('data <>'=>'','formid <>'=>'');
    $params['uniacid']=$_SESSION['uniacid'];
    $params['type']=1;
    if($_RQ['mid']){
        $params['uid']=intval($_RQ['mid']);
        $return['params']['mid']=$_RQ['mid'];
    }
    if(!empty($_RQ['plugin_name'])){
        $params['plugin_name']=trim($_RQ['plugin_name']);
        $return['params']['plugin_name']=$_RQ['plugin_name'];
    }
    if(!empty($_RQ['pageaction'])){
        $params['pageaction']=trim($_RQ['pageaction']);
        $return['params']['pageaction']=$_RQ['pageaction'];
    }
    if($_RQ['formid']){
        $params['formid like']='%id'.trim($_RQ['formid']).'i%';
        $return['params']['formid']=$_RQ['formid'];
    }
    //创建时间
    if($_RQ['createtime']){
        $time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
        $time_range=json_decode($time_range,true);
        $params['createtime between']=array($time_range['tstart'],$time_range['tend']);
        $return['params']['createtime']=$time_range;
    }
    $pindex = max(1, intval($_RQ['page']));
    $psize = 10;
    $total='';
    $list=pdo_getslice('core_account_message',$params,array($pindex,$psize),$total,$fields,'','id desc');
    if($list){
        foreach($list as &$item){
            $item['member']=cfc('core')->member_show($item['uid']);
            $item['data']=$item['data']?json_decode($item['data'],true):array();
            if($item['data'] && !$htmlitem){
                $htmlitem=$item;
            }
            if($item['posttype']==1){
                foreach($item['data'] as $dk=>$diyv){
                    $dk +=1;
                    foreach($diyv as $k=>&$v){
                        if($v['type']==5 && $v['data']){
                            $v['data'] = explode(";", $v['data']);
                            foreach($v['data'] as $k=>&$i){
                                if(empty($v['data'][$k])){
                                    unset($v['data'][$k]);
                                }
                            }
                        }
                        $item['diykey'.$k]=$v['data'];
                        //$v['type']=$formtypes[$v['type']];
                    }
                    $item['data'.$dk]=$diyv;
                }
            }else{
                foreach($item['data'] as $k=>$v){
                    if($v['type']==5 && $v['data']){
                        $v['data'] = explode(";", $v['data']);
                        foreach($v['data'] as $k=>&$i){
                            if(empty($v['data'][$k])){
                                unset($v['data'][$k]);
                            }
                        }
                    }
                    $item['diykey'.$k]=$v['data'];
                }
            }
        }
    }
    $return['list']=$list;
    $totalpage=ceil($total/$psize);
    $totalpage=intval($totalpage);
    $return['totalpage'] =$totalpage;
    $return['params']['page'] =$pindex;
    $htmls=array(
        'type'=>'list',
        'export'=>'export',
        'tabs'=>$tabs,
        'list'=>array('data'=>'list','tip'=>'[温馨提示]：建议先选择页面，再导出表单，否则表单的自定义项的名称，会错乱！','edit'=>false,'list'=>array(
             array('name'=>'title','title'=>'页面','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
            array('name'=>'createtime','title'=>'留言时间','type'=>'time'),
        ),'action'=>array(
            array('type'=>'href','op'=>'look','title'=>'留言详情'),
        )),
        'search'=>array(
            array('type'=>'choose','width'=>18,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
            array('type'=>'choose','width'=>18,'action'=>'core/account.message/diy/','name'=>'formid','title'=>'选择页面'),
            array('name'=>'createtime','width'=>15,'title'=>'提交时间','type'=>'time_range'),
        )
    );
    $item=$htmlitem;
    if($item['posttype']==1){
        foreach($item['data'] as $dk=>$diyv){
            $dk +=1;
            foreach($diyv as $k=>&$v){
                if($v['type']==5 && $v['data']){
                    $v['data'] = explode(";", $v['data']);
                    foreach($v['data'] as $k=>&$i){
                        if(empty($v['data'][$k])){
                            unset($v['data'][$k]);
                        }
                    }
                }
                $htmls['list']['list'][]=array('name'=>'diykey'.$k,'title'=>$v['title'],'type'=>'string');
                //$v['type']=$formtypes[$v['type']];
            }
            $item['data'.$dk]=$diyv;
        }
    }else{
        foreach($item['data'] as $k=>$v){
            if($v['type']==5 && $v['data']){
                $v['data'] = explode(";", $v['data']);
                foreach($v['data'] as $k=>&$i){
                    if(empty($v['data'][$k])){
                        unset($v['data'][$k]);
                    }
                }
            }
            $htmls['list']['list'][]=array('name'=>'diykey'.$k,'title'=>$v['title'],'type'=>'string');
            //$v['type']=$formtypes[$v['type']];
        }
    }
    $return['htmls']=$htmls;
    exi($return);
}
if($_RQ['op']=='look'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_message',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('请选择留言','error');
	}
	
	$member=pdo_get('core_members',array('id'=>$item['uid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
	$item['avatar'] = $member['avatar'];
	$item['nickname'] = $member['nickname'];
	$item['data']=$item['data']?json_decode($item['data'],true):array();
	$formtypes=get_form_types();
	if($item['posttype']==1){
		foreach($item['data'] as $dk=>&$diyv){
			$dk +=1;
			foreach($diyv as &$v){
				if($v['type']==5 && $v['data']){
					$v['data'] = explode(";", $v['data']);
					foreach($v['data'] as $k=>&$i){
						if(empty($v['data'][$k])){
							unset($v['data'][$k]);
						}
					}
				}
				//$v['type']=$formtypes[$v['type']];
			}
			$item['data'.$dk]=$diyv;
		}
	}else{
		foreach($item['data'] as &$v){
			if($v['type']==5 && $v['data']){
				$v['data'] = explode(";", $v['data']);
				foreach($v['data'] as $k=>&$i){
					if(empty($v['data'][$k])){
						unset($v['data'][$k]);
					}
				}
			}
			//$v['type']=$formtypes[$v['type']];
		}
	}
	
	//print_r($item);exit;
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回项目列表'),
		),
		'desc'=>'留言信息',
		'look'=>array(
				array('type'=>'string','title'=>'用户名称','name'=>'nickname','help'=>''),
				array('type'=>'image','title'=>'用户头像','name'=>'avatar','help'=>''),
				
		)
	);
	if($item['posttype']==1){
		foreach($item['data'] as $k=>&$diyv){
			$k +=1;
			$htmls['look'][]=array(
						'title'=>'表单详情'.$k,
						'type'=>'array',
						'name'=>'data'.$k,
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
						),
			);
		}
	}else{
		$htmls['look'][]=array(
						'title'=>'表单详情',
						'type'=>'array',
						'name'=>'data',
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
						),
		);
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['type']=1;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['plugin_name'])){
		$params['plugin_name']=trim($_RQ['plugin_name']);
		$return['params']['plugin_name']=$_RQ['plugin_name'];
	}
	if(!empty($_RQ['pageaction'])){
		$params['pageaction']=trim($_RQ['pageaction']);
		$return['params']['pageaction']=$_RQ['pageaction'];
	}
    //创建时间
    if($_RQ['createtime']){
        $time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
        $time_range=json_decode($time_range,true);
        $params['createtime between']=array($time_range['tstart'],$time_range['tend']);
        $return['params']['createtime']=$time_range;
    }
    $header=array(
        'id'=>'ID',
        'nickname'=>'昵称',
        'mobile'=>'电话',
    );
	$order=pdo_getall('core_account_message',$params);
	foreach($order as &$item){
		$item['createtime'] = date('Y-m-d H:i:s',$item['createtime']);
		$member=pdo_get('core_members',array('id'=>$item['uid'],'uniacid'=>$_SESSION['uniacid']),array('mobile','nickname'));
		$item['mobile'] = $member['mobile'];
		$item['nickname'] = $member['nickname'];
		$item['data']=$item['data']?json_decode($item['data'],true):array();
		$formtypes=get_form_types();
		$item['data2']='';
		if($item['posttype']==1){
			foreach($item['data'] as $dk=>&$diyv){
				$dk +=1;
				foreach($diyv as $k=>&$v){
					$item['data2'] .=$v['title']."：";
					if(is_array($v['data']) && $v['type']!=7){
						$v['data']=$v['data'][0].' '.$v['data'][1].' '.$v['data'][2].' '.$v['data'][3];
					}
					if($v['type']==7){
						$v['data']=$v['data']['province'].' '.$v['data']['city'].' '.$v['data']['district'];
					}
					$item['data2'] .=$v['data'];
					//$v['type']=$formtypes[$v['type']];
                    $header['from'.$k]=$v['title'];
                    $item['from'.$k]=$v['data'];
				}
				//$item['data2'] .="\n";
			}
		}else{
			foreach($item['data'] as $k=>&$v){
				$item['data2'] .=$v['title']."：";
				if(is_array($v['data']) && $v['type']!=7){
						$v['data']=$v['data'][0].' '.$v['data'][1].' '.$v['data'][2].' '.$v['data'][3];
				}
				if($v['type']==7){
						$v['data']=$v['data']['province'].' '.$v['data']['city'].' '.$v['data']['district'];
				}
				$item['data2'] .=$v['data'];
                $header['from'.$k]=$v['title'];
                $item['from'.$k]=$v['data'];
				//$item['data2'] .="\n";
			}
		}
	}
    $header['data2']='汇总内容';
    $header['createtime']='时间';
	cfc('tools')->export2excel($header,$order,'表单');
}
if($_RQ['op']=='diy'){
    $pindex=max(1,intval($_RQ['page']));
    $psize=10;
    $params=array('uniacid'=>$_SESSION['uniacid'],'webid'=>0,'pctype <='=>1);
    if(!empty($_RQ['title'])){
        $params['title like']='%'.trim($_RQ['title']).'%';
        $return['params']['title']=$_RQ['title'];
    }
    $total=0;
    $fields=array('id','title','createtime');
    $list=pdo_getslice('core_mobile_web',$params,array($pindex,$psize),$total,$fields,'','id desc');
    if($list){
        foreach($list as &$item){
            $item['createtime']=date('y/m/d H:i:s',$item['createtime']);
        }
    }
    $return['list']=$list;
    $totalpage=ceil($total/$psize);
    $totalpage=intval($totalpage);
    $return['totalpage'] =$totalpage;
    $return['params']['page'] =$pindex;
    $htmls=array(
        'type'=>'list',
        'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'tip'=>'这里只显示DIY页面（系统页面，头部，底部等类型页面不显示）！','list'=>array(
            array('name'=>'title','title'=>'标题','type'=>'string'),
            array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
        ),'action'=>array(
            array('type'=>'modal_result','name'=>'id','title'=>'选取'),
        )),
        'search_one'=>array('name'=>'title','add'=>false),
    );
    $return['htmls']=$htmls;
    exi($return);
}